<?php
namespace admin\controllers;


use admin\models\Admin;
use admin\models\UserInfo;
use admin\models\UserMeeting;
use Yii;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;

/**
 * Created by PhpStorm.
 * User: 沈小鱼
 * Date: 2016/7/26
 * Time: 19:37
 */
class MeetController extends BaseController{



    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['add','list','del'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }



    public function actionList(){
        $user_id = Yii::$app->request->get('user_id');
        $user = UserInfo::findOne($user_id);
        $datameettings = UserMeeting::find()->where('(mu_id='.$user_id.' or mu_targrt='.$user_id.') and UNIX_TIMESTAMP(mm_date)>'.(time()-2592000));
        $meetPages = new Pagination([
            'totalCount' =>$datameettings->count(),
            'pageSize' => '5',
            'pageSizeParam' => false,
        ]);
        $meettings = $datameettings->offset($meetPages->offset)->limit($meetPages->limit)->all();
        $others = ArrayHelper::map(UserInfo::find()->where('mi_id<>'.$user_id." and mi_sex<>'".$user['mi_sex']."'")->asArray()->all(),'mi_id','mi_name');
        return $this->renderPartial('list',[
            'user'=>$user,
            'meetPage'=>$meetPages,
            'meettings'=>$meettings,
            'targets'=>$others,
        ]);
    }


    public function actionAdd(){
        $admin = Yii::$app->user->identity->getId();
        if(empty($admin)){
            return $this->showResult(302,'用户已登出');
        }
        $user_id = Yii::$app->request->get('id');
        if(empty($user_id)){
            return $this->showResult(301,'读取用户信息失败');
        }
        $meetArr = Yii::$app->request->post('UserMeeting');
        if(empty($meetArr['mm_id'])){
            $meet = new UserMeeting();
            $meet->attributes = [
                'mm_date'=>$meetArr['mm_date'],
                'mu_id'=>$user_id,
                'mu_targrt'=>$meetArr['mu_targrt'],
            ];
        }else{
            $meet = UserMeeting::findOne($meetArr['mm_id']);
            $meet->mm_date=$meetArr['mm_date'];
            if($meet->mu_id!=$user_id){
                $meet->mu_id = $meetArr['mu_targrt'];
            }else{
                $meet->mu_targrt = $meetArr['mu_targrt'];
            }
        }
        if(!$meet->save()){
            return $this->showResult(400,'保存约会信息失败');
        }else{
            return $this->showResult(200,'成功');
        }
    }

    public function actionDel(){
        $mm_id = Yii::$app->request->post('mm_id');
        if(empty($mm_id)){
            return $this->showResult(301,'未获取到约会信息');
        }
        $admin = Yii::$app->user->identity;
        if(empty($admin)){
            return $this->showResult(302,'您的登录信息已失效，请重新登录');
        }
        $meet = UserMeeting::findOne($mm_id);
        if(empty($meet)){
            return $this->showResult(301,'该条记录已不存在');
        }
        $row = Yii::$app->db->createCommand('delete from mm_user_meeting WHERE mm_id='.$mm_id)->execute();
        if(!empty($row)){
            return $this->showResult(200,'删除成功');
        }else{
            return $this->showResult(400,'删除失败');
        }
    }

}